package models

import (
	"git.inedcn.com/ined/cmn"
	"rongyy/src/app"
	"rongyy/src/middlewares"
)

type ApplicationModel struct {
	BaseModel       `xorm:"extends"`
	Name            string            `xorm:"name" json:"name"`
	RecruitmentName string            `xorm:"recruitment_name" json:"recruitmentName"`
	Rid             int64             `xorm:"r_id" json:"r_id"`
	Sex             string            `xorm:"sex"  json:"sex"`
	Phone           string            `xorm:"phone"  json:"phone"`
	PatientId       string            `xorm:"patient_id"  json:"patient_id"`
	UnionId         string            `xorm:"union_id" json:"union_id"`
	Birthday        cmn.JsonDate      `xorm:"birthday"  json:"birthday"`
	IdNum           string            `xorm:"id_num"  json:"id_num"`
	Illness         string            `xorm:"illness"  json:"illness"`
	Disease         string            `xorm:"disease"  json:"disease"`
	DiseaseList     []DiseaseModel    `xorm:"-"  json:"diseaseList"`
	Ids             []int64           `xorm:"-"  json:"ids"`
	DiseaseStr      string            `xorm:"-"  json:"diseaseStr"`
	Contacted       string            `xorm:"contacted"  json:"contacted"`
	Address         string            `xorm:"address"  json:"address"`
	Recruitment     *RecruitmentModel `xorm:"-"  json:"recruitment"`
	ReadStatus      string            `xorm:"read_status"  json:"readStatus"`
	ReadUserName    string            `xorm:"read_user_name"  json:"readUserName"`
	ReadUserId      string            `xorm:"read_user_id"  json:"readUserId"`
}

func (sm *ApplicationModel) TableName() string {
	return "application"
}

func (sm *ApplicationModel) Insert() error {
	_, err := app.DB().Insert(sm)
	if err != nil {
		return err
	}
	return nil
}

func (sm *ApplicationModel) Update(req *ApplicationModel) error {
	_, err := app.DB().ID(req.Id).Update(req)
	if err != nil {
		middlewares.Logger.Println("ERROR - type Update failed ", err)
		return err
	}
	return nil
}

func (sm *ApplicationModel) SelectData(unionId string, indexInt int, sizeInt int, name string, from string, to string, disease string, key string, db *RecruitmentModel, status string) ([]ApplicationModel, int64, error) {
	data := make([]ApplicationModel, 0)
	s := app.DB().Table("application")
	if db != nil {
		s = s.Where("r_id= ?", db.Id)
	}
	//if patientId != "" {
	//	s = s.Where("patient_id= ?", patientId)
	//}
	if unionId != "" {
		s = s.Where("union_id= ?", unionId)
	}
	if name != "" {
		s = s.Where("name like ?", "%"+name+"%")
	}
	if disease != "" {
		s = s.Where("disease like ?", "%"+disease+"%")
	}
	if status == "全部" {
		status = ""
	}
	if status != "" {
		s = s.Where("read_status = ?", status)
	}
	if to != "" && from != "" {
		s = s.Where("created_at between ? and ?", from, to)
	}
	if key != "" {
		s = s.Where("patient_id= ? or name like ? or recruitment_name like ?", key, "%"+key+"%", "%"+key+"%")
	}
	total, err := s.Limit(sizeInt, indexInt*sizeInt).Desc("id").FindAndCount(&data)
	if err != nil {
		return nil, 0, err
	}
	return data, total, nil
}

func (sm *ApplicationModel) SelectByNameAndRid(name string, rid int64) (*ApplicationModel, error) {
	doc := &ApplicationModel{}
	has, err := app.DB().Where("name = ? and r_id=?", name, rid).Get(doc)
	if err != nil {
		middlewares.Logger.Println("find by ApplicationModel", err)
		return nil, err
	}
	if !has {
		return nil, nil
	}
	return doc, err
}

func (sm *ApplicationModel) DeleteByRId(id string) error {
	doc := &ApplicationModel{}
	_, err := app.DB().Where("r_id = ?", id).Delete(doc)
	if err != nil {
		middlewares.Logger.Println("delete by ApplicationModel", err)
		return err
	}
	return nil
}

func (sm *ApplicationModel) SelectById(id int64) (*ApplicationModel, error) {
	doc := &ApplicationModel{}
	has, err := app.DB().Where("id = ? ", id).Get(doc)
	if err != nil {
		middlewares.Logger.Println("find by ApplicationModel", err)
		return nil, err
	}
	if !has {
		return nil, nil
	}
	return doc, err
}
